package org.example.caiwumanage.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.caiwumanage.result.Result;
import org.example.caiwumanage.utils.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
public class UploadController
{
    @Autowired
    private AliOSSUtils aliossUtils;
//    //本地存贮
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
//         String file=image.getOriginalFilename();
//         String newFilename= UUID.randomUUID().toString()+file.substring(file.lastIndexOf("."));
//         //讲文件存入本电脑1
//        image.transferTo(new File("C:\\Users\\123\\Desktop\\案例.java\\",newFilename));
//
//        log.info("文件上传{}，{}，{}",username,age,image);
//        return Result.success();

//阿里云存贮
    @PostMapping("/upload")
    public Result upload (MultipartFile image) throws IOException{
    log.info("文件上传，文件名：{}",image.getOriginalFilename());
    //调用阿里云OSS工具类进行文件上传
    String url = aliossUtils.upload(image);
    log.info("文件上传完成,文件访问的url：{}",url);
    return Result.success(url) ;
    }


}
